﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Sworm
{
    public sealed class DataReader
    {
        private readonly IDataReader reader;
        public readonly Dictionary<string, int> columns;

        public DataReader(IDataReader reader)
        {
            this.reader = reader;

            columns = new Dictionary<string, int>();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                columns[reader.GetName(i)] = i;
            }
        }

        public bool Contains(string name)
        {
            return columns.ContainsKey(name);
        }

        public bool Contains(SqlIdentifier identifier)
        {
            return Contains(identifier.Unescaped);
        }

        public int GetIndex(string name)
        {
            return columns[name];
        }

        public int GetIndex(SqlIdentifier identifer)
        {
            return GetIndex(identifer.Unescaped);
        }

        public IDataReader Reader { get { return reader; } }

        public object this[SqlIdentifier index]
        {
            get { return reader[index.Unescaped]; }
        }
    }
}
